package com.m3u8.db;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import com.m3u8.db.model.M3u8Model;
import com.m3u8.db.model.TsModel;

import java.util.List;

@Dao
public abstract class TsDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    public abstract long[] insert(TsModel... tsModels);

    @Update
    public abstract int update(TsModel... tsModels);

    @Query("UPDATE tb_ts SET state = :state WHERE id = :tsId")
    public abstract int updateTsState(int state, long tsId);

    @Delete
    public abstract int delete(TsModel... tsModels);

    @Query("SELECT * FROM tb_ts")
    public abstract List<TsModel> queryForAll();

    //使用内连接查询
    @Query("SELECT tb_ts.*  from tb_m3u8 INNER JOIN tb_ts ON tb_m3u8.id=tb_ts.m3u8_id")
    public abstract List<TsModel> getTsFromM3u8();

    @Query("SELECT * FROM tb_ts WHERE m3u8_id = :m3u8Id")
    public abstract List<TsModel> getTsFromM3u8(long m3u8Id);


    @Query("SELECT * FROM tb_ts WHERE m3u8_id = :m3u8Id AND state != :state")
    public abstract List<TsModel> getTsFromM3u8NotSuccess(long m3u8Id, int state);

    // 后去百分比
    @Query("SELECT a.success * 100 / b.total FROM (SELECT COUNT(id) success FROM tb_ts WHERE m3u8_id = :m3u8Id AND state = :state) a, (SELECT COUNT(id) total FROM tb_ts WHERE m3u8_id = :m3u8Id) b")
    public abstract int getTsProcess(long m3u8Id, int state);
}
